#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
#include<json-c/json.h>

//接收音响的的回复信息
void* receive(void* arg)
{
        int fd=*(int*)arg;
        char buf[1024]={0};
        while(1)
        {
                int ret =recv(fd,buf,sizeof(buf),0);
                if(-1==ret){
                        perror("server: recv");
                        exit(1);
                }
                struct json_object* json = json_tokener_parse(buf);
                struct json_object* obj;
                json_object_object_get_ex(json,"cmd",&obj);
                char cmd[32]={0};
                strcpy(cmd,json_object_get_string(obj));
                if(!strcmp(cmd,"reply"))
                        printf("操作成功！ %s\n",buf);
                else if(!strcmp(cmd,"info"))
                        printf("%s\n",buf);
                else if(!strcmp(cmd,"reply_status"))
                        printf("%s\n",buf);
                else if(!strcmp(cmd,"reply_music"))
                        printf("%s\n",buf);
                
                memset(buf,0,sizeof(buf));
        }
        
}

int main()
{
        struct sockaddr_in server_addr;//保存服务器的信息
        struct sockaddr_in client_addr;//保存客户端的信息
        //创建socket
        int sockfd = socket(AF_INET, SOCK_STREAM,0);//ipv4地址族流式套接字
        if (-1== sockfd)
        {
                perror("socket");
                exit(-1);
        }
        //绑定信息
        bzero(&server_addr, sizeof(server_addr));//结构体清零
        server_addr.sin_family=AF_INET;//地址族，跟上面保持一致
        
        server_addr.sin_port = 8081;//端口号大于1024小于65535
        server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");//ip地址 127.0.0.1回环ip 表示本机//127.0.0.2
        int ret =bind(sockfd,(struct sockaddr *)&server_addr,sizeof(server_addr));
        if(-1 == ret)
        {
                perror("bind");
                exit(1);
        }
        
        //设置监听队列
        ret=listen(sockfd,10);
        if(-1==ret)
        {
                perror("listen");
        }

        //接受连接
        printf("等待客户端连接...\n");
        int length = sizeof(client_addr);
        int fd = accept(sockfd,(struct sockaddr *)&client_addr,(socklen_t *)&length);
        if(-1 == fd)
        {
                perror("accept");
                exit(1);
        }
        
        printf("客户端连接成功 %d!\n",fd);

        //新建线程接收回复信息
        pthread_t tid;
        ret =pthread_create(&tid,NULL,receive,&fd);        

        char buf[1024] = {0};
        while(1)
        {
                scanf("%s",buf);
                //printf("输入了\n");
                struct json_object* json = json_object_new_object();
                json_object_object_add(json,"cmd",json_object_new_string(buf));

                const char* s=json_object_to_json_string(json);
                ret=send(fd,s,strlen(s),0);
        
        }
        close(fd);      //关闭TCP连接
        close(sockfd);
        
        
        return 0;
}
 